package pkleczek.bugbrain.laboratory.elements;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

public class Synapse {
	private CircutElement input;
	private CircutElement output;

	private List<Point> waypoints;

	private final List<Float> drawablePath = new ArrayList<Float>();

	private static final Paint paint;

	private final RectF oval = new RectF();

	static {
		paint = new Paint();
		paint.setColor(Color.CYAN);
		paint.setStrokeWidth(10.0f);
	}

	public Synapse(CircutElement input, CircutElement output,
			List<Point> waypoints) {
		super();
		this.input = input;
		this.output = output;
		this.waypoints = new ArrayList<Point>(waypoints);
	}

	public void draw(Canvas canvas, int tileSize) {
		// input -> [waypoints] -> output

		int halfSize = tileSize / 2;

		drawablePath.clear();

		drawablePath.add((float) (input.getPosition().x * tileSize + halfSize));
		drawablePath.add((float) (input.getPosition().y * tileSize + halfSize));

		for (Point p : waypoints) {
			drawablePath.add((float) (p.x * tileSize + halfSize));
			drawablePath.add((float) (p.y * tileSize + halfSize));
			drawablePath.add((float) (p.x * tileSize + halfSize));
			drawablePath.add((float) (p.y * tileSize + halfSize));
		}

		drawablePath
				.add((float) (output.getPosition().x * tileSize + halfSize));
		drawablePath
				.add((float) (output.getPosition().y * tileSize + halfSize));

		float[] arr = new float[drawablePath.size()];
		for (int i = 0; i < drawablePath.size(); i++) {
			arr[i] = drawablePath.get(i);
		}

		canvas.drawLines(arr, paint);
	}

}
